package net.bplaced.streetfighter.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.bplaced.streetfighter.MatchDetailActivity;
import net.bplaced.streetfighter.R;
import net.bplaced.streetfighter.fragment.MatchDetailFragment;
import net.bplaced.streetfighter.lib.model.Match;
import net.bplaced.streetfighter.lib.utils.RuntimeData;
import net.bplaced.streetfighter.lib.utils.RuntimeDataKey;
import net.bplaced.streetfighter.lib.utils.RuntimeDataObject;
import net.bplaced.streetfighter.lib.utils.StreetfighterUtils;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class MatchAdapter
        extends AbstractAdapter<Match>
        implements OnClickListener {

    private final Fragment fragment;

    private boolean isTablet;

    public MatchAdapter(final Context context, final List<Match> matches, final Fragment fragment) {

        super(context, matches);

        this.fragment = fragment;

        if (RuntimeData.getInstance().get(RuntimeDataKey.IS_TABLET) != null) {
            this.isTablet = (Boolean) RuntimeData.getInstance().get(RuntimeDataKey.IS_TABLET).getData();
        }
    }

    @Override
    public void onClick(View v) {

        final Match match = (Match) v.getTag();
        RuntimeData.getInstance().put(RuntimeDataKey.MATCH, new RuntimeDataObject<Match>(match));

        StreetfighterUtils.changeContent(this.fragment.getActivity().getFragmentManager(), this.context, this.isTablet,
                R.id.main_detail_container, new MatchDetailFragment(), new Intent(this.context,
                        MatchDetailActivity.class));
    }

    @Override
    View getView(int position, View convertView, LayoutInflater inflater) {

        final Match m = this.items.get(position);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_item_match, null);
        }

        // Opponent field
        final TextView oppField = (TextView) convertView.findViewById(R.id.match_adapter_item_opponent);
        oppField.setText(m.opponent);

        final Date tempDateTime = StreetfighterUtils.convertToDate(m.matchDate, m.matchTime);

        // Date
        final TextView dateField = (TextView) convertView.findViewById(R.id.match_adapter_item_date);
        dateField.setText(new SimpleDateFormat("dd.MM.yyyy").format(tempDateTime));

        // Time
        final TextView timeField = (TextView) convertView.findViewById(R.id.match_adapter_item_time);
        timeField.setText(new SimpleDateFormat("HH:mm").format(tempDateTime));

        // Category field
        final TextView categoryField = (TextView) convertView.findViewById(R.id.match_adapter_item_category);

        if (m.matchCategory == 2) {
            categoryField.setText(R.string.match_heim);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_heim));
        } else if (m.matchCategory == 3) {
            categoryField.setText(R.string.match_auswaerts);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_auswaerts));
        } else if (m.matchCategory == 4) {
            categoryField.setText(R.string.match_pokal);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_pokal));
        } else if (m.matchCategory == 5) {
            categoryField.setText(R.string.match_playoff);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_playoff));
        } else if (m.matchCategory == 6) {
            categoryField.setText(R.string.match_testspiel);
            categoryField.setBackgroundDrawable(this.context.getResources().getDrawable(
                    R.drawable.category_gradient_testspiel));
        }

        convertView.setTag(m);
        convertView.setOnClickListener(this);
        this.fragment.registerForContextMenu(convertView);

        return convertView;
    }
}